﻿using System;
using System.Web.Routing;

namespace Smarly.Web.RouteConstraints
{
	public class DomainRouteConstraint : IRouteConstraint
	{
		#region IRouteConstraint Members

		public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
		{
			string requesSubDomain = httpContext.Request.Headers["host"].Split('.')[0];
			string subdomain = (string)values["subdomain"];
			return routeDirection == RouteDirection.UrlGeneration || (!String.IsNullOrEmpty(subdomain) && subdomain.Equals(requesSubDomain));
		}

		#endregion
	}
}